package com.chb.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.chb.entity.Warehouse;
import com.chb.service.WarehouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Controller
public class WarehouseController {
	//注入service
		@Autowired
		private WarehouseService warehouseService;
		@RequestMapping("/selectAllWarehouses")
		public String selectAllWarehouses(@RequestParam(required=true,defaultValue="1") int pageNum,@RequestParam(required=true,defaultValue="5") int pageSize,Model model) {
			PageHelper.startPage(pageNum, pageSize);
			List<Warehouse> list = this.warehouseService.selectAllWarehouses();
			PageInfo<Warehouse> page=new PageInfo<Warehouse>(list);	
			System.out.println("Controller层");
			System.out.println(list);
			model.addAttribute("warehouselist", list);
			model.addAttribute("page", page);
			return "/warehouselist.jsp";
			
		}
		//根据货物名称查询货物信息
		@RequestMapping("/selectWarehouses")
		public String selectWarehouses(String cargoname,Model model) {

			List<Warehouse> wh = this.warehouseService.selectWarehouses(cargoname);
			System.out.println("Controller层");
			System.out.println(wh);
			model.addAttribute("wh", wh);
			return "/warehouselist.jsp";

		}
		//根据id查询货物信息
				@RequestMapping("/toupdateWarehouse")
				public String toupdateProduct(Integer id,Model model) {

					Warehouse warehouse = this.warehouseService.selectWarehouse(id);
					System.out.println("Controller层");
					model.addAttribute("warehouse", warehouse);
					return "/warehouses.jsp";

				}
		@RequestMapping("/doupdateWarehouse")
		public String doupdateWarehouse(Warehouse warehouse) {

			boolean result = this.warehouseService.updateWarehouse(warehouse);
			if(result) {
				return "forword:selectAllWarehouses";
			}else {
				return "/warehouses.jsp";
			}
			
		}
		@RequestMapping("/deleteWarehouse")
		public String deleteWarehouse(Warehouse warehouse) {

			boolean result = this.warehouseService.deleteWarehouse(warehouse);
			if(result) {
				return "forword:selectAllWarehouses";
			}else {
				return "/warehouses.jsp";
			}

		}
}
